<template>
  <div class="pie-container">
    <div id="myChart2" :style="{ width: '800px', height: '400px' }"></div>
  </div>
</template>

<script>
export default {
  name: 'Pie',
  data () {
    return {
      hotTags: []
    }
  },
  methods: {
    // 获取热门标签数据
    async initTagsInfo () {
      const { data: res } = await this.$api.getHotTags()
      // console.log(res)
      // this.hotTags = res.data
      for (let i = 0; i < res.data.length; i++) {
        const element = res.data[i]
        const temp = {
          value: parseFloat(element.tag_number),
          name: element.tag_name
        }
        this.hotTags[i] = temp
        // console.log(temp)
      }
      // console.log(this.hotTags)
      this.drawLine()
    },
    drawLine () {
      // 基于准备好的dom，初始化echarts实例
      var myChart2 = this.$echarts.init(document.getElementById('myChart2'))
      // 绘制图表
      myChart2.setOption({
        title: {
          text: '热门标签统计',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: '50%',
            data: this.hotTags,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    }
  },
  created () {
    this.initTagsInfo()
  },
  mounted () {
    // this.drawLine()
  }
}
</script>

<style></style>
